﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ Import Namespace="YoProgramo.Domain.BusinessLogic.Service"%>
<%@ Import Namespace="YoProgramo.Domain.Core.Entities"%>
<%@ Import Namespace="YoProgramo.Domain.BusinessLogic.Extensions"%>
<%@ Import Namespace="YoProgramo.Domain.Core.Interfaces"%>
<%@ Import Namespace="YoProgramo.BusinessLogic.Service"%>
<%@ Import Namespace="Microsoft.Practices.ServiceLocation"%>
<%@ Import Namespace="System.Globalization"%>

  <script type="text/javascript">
    // to set WMD's options programatically, define a "wmd_options" object with whatever settings
    // you want to override.  Here are the defaults:
    wmd_options = {
      // format sent to the server.  Use "Markdown" to return the markdown source.
      output: "Markdown",

      // line wrapping length for lists, blockquotes, etc.
      //lineLength: 40,

      // toolbar buttons.  Undo and redo get appended automatically.
      buttons: "bold italic | link blockquote code image | ol ul heading hr",

      // option to automatically add WMD to the first textarea found.  See apiExample.html for usage.
      autostart: true
    };
  </script>
  <script src="/Scripts/wmd/wmd.js" type="text/javascript"></script> 



<%
  if (Model != null && Model is Story && ((Story)Model).Comments != null)
    {
        bool isStoryAutor = false;
        bool isCommentAutor = false;
        bool isAdministrator = false;
        bool showSpamLink = false;
        IUser user = null;
        if (HttpContext.Current.User.Identity.IsAuthenticated) {
            user = ServiceLocator.Current.GetInstance<IUserService>().GetByUserName(HttpContext.Current.User.Identity.Name);
            isStoryAutor = Html.IsAuthor<Story>(HttpContext.Current.User.Identity.Name, ((Story)Model));
            isAdministrator = user.IsInRole(YoProgramo.Domain.Core.Roles.Administrator);
            
        }

        ICommentService commentService = ServiceLocator.Current.GetInstance<ICommentService>();
        IList<Comment> comments = commentService.GetCleanComments(((Story)Model).Comments);

        foreach (Comment c in comments)
        {            
            var cssclass = c.IsBestComment ? "BestCommentView" : "CommentView";
            var bestcomment = c.IsBestComment ? "Mejor Respuesta" : "";

            if (HttpContext.Current.User.Identity.IsAuthenticated) {
              isCommentAutor = Html.IsAuthor<Comment>(HttpContext.Current.User.Identity.Name, c);
              showSpamLink = !Html.IsAuthor<Comment>(HttpContext.Current.User.Identity.Name, c);
            }          
%>


<div style="margin-top: 20px;">
    <div class="storyViewHeadBox <%= cssclass %>">
    
        <div class="featuresBox featuresBoxComment">
          <div class="RankBoxvot RankBoxvotViewComment">
            <div id="coRank-<%=c.Id %>" class="storyRank storyRankViewComment"><%= c.GetScore() %>   
            </div>                  
            <div class="vot votcomment">
              <a href="javascript: VoteFx('/Comment/VoteUp/<%= c.Id %>', '#coRank-<%=c.Id %>');">+</a>&nbsp;&nbsp;<a name="menoslinkComment" href="javascript: VoteFx('/Comment/VoteDown/<%=c.Id %>', '#coRank-<%=c.Id %>');">-</a>
            </div>             
          </div>
          <div id="bestcomment-<%=c.Id%>" class="bestCommentIcon">                   
            <%
              if(c.IsBestComment)
              {
                %>
                Mejor<br />Respuesta
                <%
              }else
              {
                if (isStoryAutor && user != null && c.ByUser != user)
                {
                  %>
                  <a href="javascript: SetBestComment('/Comment/SetBest/<%=c.Id%>', '#bestcomment-<%=c.Id%>');">¿Es mejor<br />respuesta?</a>
                  <%
                }
              }
  %>          
          </div>          
        </div>    
      
      <div class="storyviewview ViewStoryComment">
          <%= c.HtmlBody %>             
          <div class="storyHeadLinks">             
                <%if (showSpamLink) {%><a href="javascript: SetSpam('/Comment/SetSpam/<%=c.Id%>');">Spam</a><%}%> <%if (isCommentAutor || isAdministrator) {%> | <%=Html.RouteLink("Edit", "Default", new { controller = "Comment", action = "Edit", id = c.Id})%>  <%}%>                                                  
          </div>
          
          <div class="storyviewUser CommentViewUser"> 
            <b>Fecha:</b> <%= c.CommentedAt.ToString("d MMMM yyyy", CultureInfo.CreateSpecificCulture("es-AR"))%>            
            <div class="userpic">
              <%= Html.RouteLink(c.ByUser.UserName, "Default", new { action = "ViewProfile", controller = "User", id = c.ByUser.Id })%>
              <%= Html.Gravatar(c.ByUser.Email, new { s = "32", r = "pg" }, new { @class = "FotoUsuario", alt = "Foto" })%>
            </div>                                                           
            <b><%= c.ByUser.GetReputation()%></b>
          </div>                     
      </div>            
    </div>
</div>          
      <%
        }
    
    %>
<div class="ViewStoryYouAnswer">
    <h2>Tu Respuesta</h2>
    <% using (Html.BeginForm("Create","Comment")) {%>    
    <%= Html.ValidationSummary("Un error ha ocurrido y no fue posible agregar tu respuesta. Chequea los campos.")%> 
    
        <% = Html.AntiForgeryToken() %>      
        
        <%=Html.Hidden("storyid", ((Story)Model).Id)%>
       
       <div class="textAreaBox">
          <p>
            <%= Html.TextArea("comment.TextBody", new { style = "width:97%; height:330px;" })%>
          </p>                                                       
          <div class="wmd-preview"></div>
                 
          <p>
              <input type="submit" value="Enviar Respuesta" />
          </p>
      </div>
    <% } %>
</div>     
        
    <%
    }
%>
   